//
//  ViewController.m
//  UICollectionView文字轮播
//
//  Created by 梁森 on 2018/4/11.
//  Copyright © 2018年 梁森. All rights reserved.
//

#import "ViewController.h"

#import "CustomCollectionViewCell.h"
#define collectinViewCellID @"collectionViewCell"
#define scrollMaxSections 20

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view addSubview:self.collectionView];
    [self addTimer];
}

#pragma mark - - - UICollectionView 的 dataSource、delegate方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return scrollMaxSections;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell * cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:collectinViewCellID forIndexPath:indexPath];
    NSInteger row = indexPath.row;
    if (row % 2 == 0) {
        cell.backgroundColor = [UIColor greenColor];
    }else{
        cell.backgroundColor = [UIColor yellowColor];
    }
    cell.lbl.text = [NSString stringWithFormat:@"这是第%lu条数据", indexPath.row];
    cell.lbl.textAlignment = NSTextAlignmentCenter;
    return cell;
}
#pragma mark --- 点击UICollectionView上的item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"row:%lu", indexPath.row);
    
}
#pragma mark - - - NSTimer
- (void)addTimer {
    [self removeTimer];
    self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(beginUpdateUI) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)beginUpdateUI {
//    return;
    // 1、当前正在展示的位置
    NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
//    NSLog(@"current:%lu", currentIndexPath.row);
    // 马上显示回最中间那组的数据
    NSIndexPath *resetCurrentIndexPath = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:0.5 * scrollMaxSections];
    [self.collectionView scrollToItemAtIndexPath:resetCurrentIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
    
    // 2、计算出下一个需要展示的位置
    NSInteger nextItem = resetCurrentIndexPath.item + 1;
    NSInteger nextSection = resetCurrentIndexPath.section;
    if (nextItem == 5) {
        nextItem = 0;
        nextSection++;
    }
    
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
//    NSLog(@"next:%lu", nextIndexPath.row);
    // 3、通过动画滚动到下一个位置
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}

- (void)removeTimer {
    [_timer invalidate];
    _timer = nil;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, 50);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 50) collectionViewLayout:_flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.scrollEnabled = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor redColor];
        [_collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:collectinViewCellID];
    }
    return _collectionView;
}

@end
